/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_State.c
 *  
 *  Created on: 2022年10月17日
 *      Author: 64435
 */

#include "LOS_State.h"


/* RTOS执行类标志量 */
uint8_t RTOS_FLAG_Update1 = 0;



uint8_t RTOS_FLAG_FATFS0 = 0;



/* 整星姿态信息 */
//uint16_t LOS_ModeAcsIn = Damping;  //姿态模式，指令模式
float LOS_Targetq[4] = {1, 0, 0, 0};
float LOS_Targetpos[3] = {0, 0, 0};         //目标经度、纬度、地心距


/* 单机遥测状态 */


/* 飞行程序解锁控制结构体 */
RSEnDeCode_t FT_Struct;  //上电后，从FLASH中读取一次并存储，如果值是HOTKNIFEON就开启热刀操作，如果HOTKNIFEOFF就关闭热刀操作


/* OBC状态 */
uint8_t LOS_CPU;

#ifdef NOW_FLASH0
uint8_t LOS_FLASH_ID = Fapi_FlashBank0;
#endif

#ifdef NOW_FLASH1
uint8_t LOS_FLASH_ID = Fapi_FlashBank1;
#endif

//MAX14830芯片状态
uint8_t MAX14830_SELFSTA1[2]={0,0};
uint8_t MAX14830_SELFSTA2[2]={0,0};

//整星软件保护状态标志
uint8_t LOS_SAT_SoftSafeMode = 0;

//单机重启时间（从因异常下电到重新上电的间隔周期数）
uint8_t LOS_HL_RBTP = 40;
//重启恢复时间（重启后的保护性计数）
uint8_t LOS_HL_RECOVP = 100;

//安全模式
uint8_t LOS_SAT_SafeModeCNT = 0;
//解析遥控指令计数
uint16_t LOS_TC_READ = 0;
//执行遥控指令计数
uint16_t LOS_TC_EX = 0;
//最后一条执行指令
uint32_t LOS_TC_LASTCMD = 0;
//最近一条延时遥控指令
uint32_t LOS_TC_FirstDelayCMD = 0;
//最近一条延时遥控指令执行时刻
uint32_t LOS_TC_FirstDelayTime = 0;
//收到最近一条遥控指令的时间
uint32_t LOS_TC_LASTCMDTIME = 0;

//当前任务号
uint16_t LOS_TC_NOWTask = 0;
//当前任务执行时刻
uint32_t LOS_TC_NOWTaskTime = 0;
//待执行任务计数
uint32_t LOS_TC_TaskNum = 0;

//星务发送PCDU指令计数
uint16_t LOS_PCDU_CMDCNT = 0;
//PCDU执行指令计数
uint16_t LOS_PCDU_EXCMDCNT = 0;
//PCDU上一条执行指令
uint16_t LOS_PCDU_LASTCMD = 0;

//单机通讯状态遥测字
uint8_t LOS_TM_UnitSta[3] = {0XFF, 0XFF, 0XFF};
//平台状态遥测字
uint8_t LOS_TM_SATSta = 0;

//飞轮输出标志位 0XAA允许输出，0X55屏蔽输出
uint8_t LOS_FW_PERMIT = 0XAA;
//磁力矩器输出标志位 0XAA允许输出，0X55屏蔽输出
uint8_t LOS_MT_PERMIT = 0XAA;

//复位单机nID
uint8_t LOS_TM_RBTnID = 0;
//单机复位时刻
uint32_t LOS_TM_RBTTime = 0;
/****** 对地遥测用全局变量 ******/
//遥测包单机编号(默认为星务)
uint8_t LOS_TM_ID[6] = {0xf0,0xf0,0xf0,0xf0,0xAA,0xAA};
//遥测包类型号
uint8_t LOS_TM_Type[6] = {0x00,0x01,0x02,0x03,0X00,0X01};
//回传即时遥测包数
uint16_t LOS_TM_PackNum[6] = {0,0,0,0,0,0};
//遥测频率控制计数器上限
uint16_t LOS_TM_FreCtrl;
//遥测间隔时间(单位s),发四包遥测需要2s,间隔默认28s，保证30s发一次
uint16_t LOS_TM_InvTime = 28;
//调整遥测间隔持续时间
uint16_t LOS_TM_AdjustInvDur = 0;
//数传下传即时遥测控制 0xAA开 0x55关
uint8_t LOS_DT_TMPERMIT = 0X55;

//照片传输模式 (兼职倒计时功能)
uint16_t LOS_TM_CMOSTRAN = 0;

//SD卡中的上注文件大小
uint32_t LOS_UPFILE_SIZE = 0;

/******** 测试模式用全局变量 ********/
//进入测试模式标志位
uint8_t LOS_TESTM_FLAG = 0;
//测试模式需要的时长
uint32_t LOS_TESTM_DUR = 0;
//记录进入测试模式的时刻
uint32_t LOS_TESTM_ENT = 0;
//测试模式剩余时长
uint32_t LOS_TESTM_LEFT = 0;

/**********************************/

/******* 上注程序相关量 *******/
//设置将要从测控/数传中读取的文件号
uint8_t LOS_UPLOAD_FileNum = 0x00;
//将要从测控中读取的文件大小
uint32_t LOS_UPLOAD_FileSize = 0;
//启动从测控读取文件标志(状态机驱动量)  0:未启动 1:初始化：设置待操作文件 2:初始化：设置目标nID 3:正在读取 4:读取完成 5:地面命令中止读取 11:错误:文件未清空 12:错误:文件号不一致 13:错误:文件大小不一致 14:错误:文件系统异常 15:错误:传输CRC错误
uint8_t LOS_UPLOAD_TC2OBC = 0;
//启动从数传读取文件标志(状态机驱动量)  0:未启动 1:初始化：设置待操作文件 2:初始化：设置目标nID 3:正在读取 4:读取完成 5:地面命令中止读取 11:错误:文件未清空 12:错误:文件号不一致 13:错误:文件大小不一致 14:错误:文件系统异常 15:错误:传输CRC错误
uint8_t LOS_UPLOAD_DT2OBC = 0;
//执行节奏控制器，配合状态机使用
uint8_t LOS_UPLOAD_CNT = 0;
//读取数据块长度(默认240字节)
uint32_t LOS_UPLOAD_BlockSize = TMTC2SD_UPFSIZE;
//读取偏移地址
uint32_t LOS_UPLOAD_OffSet = 0;
//读取数据块个数
uint32_t LOS_UPLOAD_BlockCnt = 0;
//已读取到的文件大小
uint32_t LOS_UPLOAD_FileReceived = 0;
//读取完成时刻
uint32_t LOS_UPLOAD_ReadFinishTime = 0;
//烧写完成时刻
uint32_t LOS_UPLOAD_DownFinishTime = 0;
//
uint8_t STATE_UPLOAD_OPENFILE = 0;     //0:成功 1:失败
//
uint8_t STATE_UPLOAD_SD2FLASHDONE = 0; //0:未完成  1:已完成
//程序烧写允许位
uint8_t PERMIT_UPLOAD_SD2FLASH = 2;    //0:禁止 1:允许 2:初始化值，也表示禁止
//跳转至另一FLASH允许
uint8_t PERMIT_UPLOAD_JUMP = 0;
//最后一包标志
uint8_t LOS_UPLOAD_LASTPACK = 0;

//跳转地址
uint32_t Jump_Addr_FLASH = 0x00000000;   //跳转地址
//高频线程状态量（可用于CRC32计算功能）
uint8_t LOS_HFT_STA = 0;
//SD卡中存储的上注程序的CRC32值
uint32_t CRC32_SD_SAVE = 0;
//OBC计算的SD卡中存储上注程序的CRC32值
uint32_t CRC32_SD_OBCCAL = 0;
//测控/数传传输过来的CRC32值
uint32_t CRC32_TC_TRAN = 0;
//OBC计算SD卡中CRC32时的偏移地址
uint32_t CRC32_SD_OFFSET = 0;
//从SD卡向FLASH写入时剩余文件大小
uint32_t LOS_SD2FLASH_SIZE = 0;

//

//
uint16_t CTRL_TM_CNT = 0;
uint16_t EPS_TM_CNT = 0;
//MAX14830重新上电标志位
uint8_t MAX_RESET_FLAG = 0;
uint16_t MAX_RESET_CNT = 0;  //MAX14830自动复位计数

//进入姿控程序次数
uint32_t AOCS_Enter_Times = 0;

//调试量物理接口选择 0:CAN 1:UART
uint8_t DBG_PORT = 0;

//
hetSIGNAL_t signal_test;
uint32_t test_bit = 0;

//磁力矩器允许采集标志位
uint8_t LOS_MagTrqPermit = 0;

//小相机照片存储用全局变量
uint8_t CMOS_PICCNT = 0;     //SD卡已经存储的照片数量
uint32_t CMOS_TOTALSIZE = 0; //SD卡中已经存储的照片大小
uint32_t CMOS_FILESIZE = 0;  //照片文件总大小
uint32_t CMOS_NOWSIZE = 0;   //当前正在写的这张照片的大小 
uint8_t CMOS_LASTFRAME = 0X55; //照片传输最后一帧标志,0xAA表示该帧为最后一帧，其余值表示不是最后一帧（一般用0x55）
uint32_t CMOS_PICSIZE = 0;    //将要传输的照片的大小，每次传输前获取
uint8_t LOS_CMOS_CAM2OBC = 0; //0:未在传输 1:正在传输 2:传输完成 3:传输超时 4:文件系统错误
uint8_t LOS_CMOS_PICNUM = 0XFF;  //要从SD卡传到地面的照片编号
uint16_t LOS_CMOS_Invbuf = 0; //向地面发送照片时，计算机会自动将LOS_TM_InvTime改成1，发完了再改回原来的值，这个变量用来暂存LOS_TM_InvTime原来的值
uint8_t LOS_CMOS_OBC2TM = 0; //0:未在传输 1:正在传输 2:传输完成 3:传输异常（文件打开失败）
uint8_t LOS_CMOS_NUMBUF = 0; //要查询大小的照片编号
uint32_t LOS_CMOS_OBC2TCLEFT = 0; //OBC到测控照片剩余包数

//上注轨道信息
float LOS_ORB_OE[6];
float LOS_ORB_RV[6];
uint8_t LOS_ORB_UPFLAG = 0X55;

//14830测试变量
uint8_t MAX14830_CHECKTEST[12];

//PCDU测试变量,前4个字节是配电的,后4个字节是热控的，然后是计算机算的CRC PD TC
uint8_t PCDU_CRC8CHECK[10];

//开始/停止向测控请求工参 0X55禁止 0xAA允许
uint8_t TMTC_PERMITARG = 0XAA;

//热刀操作
uint8_t LOS_TK_SEL[8] = {0, 0, 0, 0, 0, 0, 0, 0}; //0XAA操作 0X55操作完了 0X00还没操作

//上注文件写入基地址
uint32_t LOS_UP_BASEADDR = 0; 
